package it.unibo.contact.domoticaContact;
import it.unibo.is.interfaces.*;

import it.unibo.is.interfaces.platforms.*;
import it.unibo.contact.platformuv.LindaLike;
import it.unibo.contact.platformuv.*;
/*
*  Abstract class Subject
*/
public abstract class Subject extends Thread implements IContactSystem{

protected ILindaLike core  ;
protected IOutputView view = null;
protected IBasicEnv env = null; 
protected final boolean distributedSpace = false;
 	public Subject() throws Exception{
	}
	
	public void setEnv(IBasicEnv env) throws Exception{
 		this.env = env;		
		initGui();
		initSupport(view);		
	}
 

public void run(){
	try { 
		doJob(); 
		showMsg(" ... sleep for a while before ending");
		Thread.sleep(2000);
		terminate();
	} catch (Exception e) {
		showMsg("Subject ERROR " + e.getMessage());
	}
}

abstract public void doJob() throws Exception;
abstract public void terminate() throws Exception;
abstract protected void initGui() throws Exception;

protected void initSupport(IOutputView view){
    core = LindaLike.getSpace(view);
//    support = new PlatformContact(view);   
}

protected boolean isSignal( String msgId ){
	return false;		 	  
}
 
 /* ==================================
	UTILS
 ================================== */
	public  void showMsg( String m){
		MsgUtil.showMsg(view, this.getName() + " " + m );
	}
	
	public void showMsg( IMessage m){
		showMsg("msg: ||" +  m.msgContent() + "|| from " + m.msgEmitter() );
	}
	
	public  void showMessage( String comment,  IMessage m){
	if( m != null) {
		String msgEmitter 	= m.msgEmitter();
		String msgId 		= m.msgId();
		String msgContent 	= m.msgContent();
		String msgNum 		= m.msgNum();

		MsgUtil.showMsg(view, this.getName() + " " + comment + ": " + m + "\n\t" +
			"msgEmitter="+ msgEmitter  + " msgId="+ msgId +
			" msgContent=" + msgContent + " msgNum=" + msgNum);
	}		  
	}  
	
  	
}		
